package com.j.lemon.learn;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.stereotype.Controller;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * @author lijunjun
 * @title: ControllerScan
 * @description: TODO
 * @date 2023/8/316:21
 */
public class ControllerScan {
    public static void main(String[] args) {
        String basePackage = "com.ais.iam.configure.controller"; // 设置你想要扫描的包路径
        Set<String> controllerPaths = scanControllers(basePackage);
        for (String path : controllerPaths) {
            System.out.println(path);
        }
    }

    public static Set<String> scanControllers(String basePackage) {
        Set<String> controllerPaths = new HashSet<>();
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
        provider.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
        provider.addIncludeFilter(new AnnotationTypeFilter(RestController.class));
        Set<BeanDefinition> components = provider.findCandidateComponents(basePackage);
        for (BeanDefinition component : components) {
            try {
                Class<?> clazz = Class.forName(component.getBeanClassName());
                RequestMapping classRequestMapping = clazz.getAnnotation(RequestMapping.class);
                String classPath = "";
                if (classRequestMapping != null && classRequestMapping.value().length > 0) {
                    classPath = classRequestMapping.value()[0];
                    Method[] methods = clazz.getDeclaredMethods();
                    for (Method method : methods) {
                        GetMapping methodRequestMapping = method.getAnnotation(GetMapping.class);
                        if (methodRequestMapping != null && methodRequestMapping.value().length > 0) {
                            for (String methodPath : methodRequestMapping.value()) {
                                String fullPath = classPath + methodPath;
                                controllerPaths.add(fullPath);
                            }
                        }else{
                            PostMapping postMapping = method.getAnnotation(PostMapping.class);
                            if (postMapping != null && postMapping.value().length > 0) {
                                for (String methodPath : postMapping.value()) {
                                    String fullPath = classPath + methodPath;
                                    controllerPaths.add(fullPath);
                                }
                            }
                        }

                    }
                }


            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return controllerPaths;
    }
}
